﻿using System;

namespace FunctionalCLR.Core.Functions.Maths {
    public class RandomNode : OperatorNode {
        public RandomNode()
            : base() {
            Value = "random";
            Precedence = 3;
        }

        public override object Evaluate() {
            Random ran = new Random();

            switch (this.Children.Count) {
                case 0:
                    return ran.Next();
                case 1:
                    int maxValue
                        = Convert.ToInt32(this.Children[0].Evaluate());
                    return ran.Next(maxValue);
                case 2:
                    maxValue
                        = Convert.ToInt32(this.Children[1].Evaluate());
                    int minValue
                        = Convert.ToInt32(this.Children[0].Evaluate());
                    return ran.Next(minValue, maxValue);
                default:
                    throw new NotSupportedException();
            }
        }
    }
}
